package cn.llin.dlxx.service.impl;

import java.util.List;

import cn.llin.common.json.JSONObject;
import cn.llin.common.utils.DateUtils;
import cn.llin.dlxx.domain.DataVehicleWarning;
import cn.llin.dlxx.mapper.DataVehicleWarningMapper;

import cn.llin.websocket.WebSocketUsers;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.llin.dlxx.service.IDataVehicleWarningService;
import cn.llin.common.core.text.Convert;

import javax.annotation.PostConstruct;

/**
 * 数据仓库——车辆预警数据信息Service业务层处理
 * 
 * @author llin
 * @date 2023-02-22
 */
@Service
public class DataVehicleWarningServiceImpl implements IDataVehicleWarningService {

    @Autowired
    private DataVehicleWarningMapper dataVehicleWarningMapper;
//
//    @Autowired
//    private WebSocketServlet webSocketServlet;

    /**
     * 查询数据仓库——车辆预警数据信息
     * 
     * @param vehicleWarningId 数据仓库——车辆预警数据信息主键
     * @return 数据仓库——车辆预警数据信息
     */
    @Override
    public DataVehicleWarning selectDataVehicleWarningByVehicleWarningId(String vehicleWarningId)
    {
        return dataVehicleWarningMapper.selectDataVehicleWarningByVehicleWarningId(vehicleWarningId);
    }

    /**
     * 查询数据仓库——车辆预警数据信息列表
     * 
     * @param dataVehicleWarning 数据仓库——车辆预警数据信息
     * @return 数据仓库——车辆预警数据信息
     */
    @Override
    public List<DataVehicleWarning> selectDataVehicleWarningList(DataVehicleWarning dataVehicleWarning)
    {
        return dataVehicleWarningMapper.selectDataVehicleWarningList(dataVehicleWarning);
    }

    @Override
    public List<DataVehicleWarning> selectDataVehicleWarningLists() {
        return dataVehicleWarningMapper.selectDataVehicleWarningLists();
    }

    /**
     * 新增数据仓库——车辆预警数据信息
     * 
     * @param dataVehicleWarning 数据仓库——车辆预警数据信息
     * @return 结果
     */
    @Override
    public int insertDataVehicleWarning(DataVehicleWarning dataVehicleWarning)
    {
        return dataVehicleWarningMapper.insertDataVehicleWarning(dataVehicleWarning);
    }

    /**
     * 修改数据仓库——车辆预警数据信息
     * 
     * @param dataVehicleWarning 数据仓库——车辆预警数据信息
     * @return 结果
     */
    @Override
    public int updateDataVehicleWarning(DataVehicleWarning dataVehicleWarning)
    {
        dataVehicleWarning.setUpdateTime(DateUtils.getNowDate());
        return dataVehicleWarningMapper.updateDataVehicleWarning(dataVehicleWarning);
    }

    /**
     * 批量删除数据仓库——车辆预警数据信息
     * 
     * @param vehicleWarningIds 需要删除的数据仓库——车辆预警数据信息主键
     * @return 结果
     */
    @Override
    public int deleteDataVehicleWarningByVehicleWarningIds(String vehicleWarningIds)
    {
        return dataVehicleWarningMapper.deleteDataVehicleWarningByVehicleWarningIds(Convert.toStrArray(vehicleWarningIds));
    }

    /**
     * 删除数据仓库——车辆预警数据信息信息
     * 
     * @param vehicleWarningId 数据仓库——车辆预警数据信息主键
     * @return 结果
     */
    @Override
    public int deleteDataVehicleWarningByVehicleWarningId(String vehicleWarningId)
    {
        return dataVehicleWarningMapper.deleteDataVehicleWarningByVehicleWarningId(vehicleWarningId);
    }

    private Long lastCount = -1L;

    private boolean stopMe = true;
    public void stopMe() {
        stopMe = false;

    }

    @PostConstruct
    @Override
    public long countAll() {
        JSONObject jsonObject = new JSONObject();

        new Thread(()->{
            try {
                Thread.sleep(15000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            while (stopMe) {
                Long count = dataVehicleWarningMapper.countAll();
                if (!lastCount.equals(count)) {
                    List<DataVehicleWarning> dataVehicleWarnings = dataVehicleWarningMapper.selectDataVehicleWarningLists();
                    jsonObject.put("dataVehicleWarnings",dataVehicleWarnings);
                    try {
//                        webSocketServlet.sendMessage(JSON.toJSONString(jsonObject));
                       WebSocketUsers.sendMessageToUsersByText(JSON.toJSONString(jsonObject));
                        lastCount=count;


                    } catch (Exception e) {
                        System.out.println( e);
                    }
                }
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        return lastCount;
    }

}
